package com.xiaoxin.project.system.service.impl;

import com.xiaoxin.project.system.domain.SysOrder;
import com.xiaoxin.project.system.service.SysOrderService;
import org.apache.shardingsphere.api.hint.HintManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 广播表测试
 * @author ZhangXX
 * @version 1.0
 * @data 2021/5/18 22:49
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test4")
public class test4 {
    @Autowired
    private SysOrderService sysOrderService;

    @Test
    public void test() {
        hintSharding();
        List<SysOrder> sysOrders = sysOrderService.selectSysOrderList(new SysOrder());
        System.out.println(sysOrders);
    }

    private void hintSharding() {
        // 清除掉上一次的规则，否则会报错
        HintManager.clear();
        // HintManager API 工具类实例
        HintManager hintManager = HintManager.getInstance();
        // 设置数据库的分片健()
        hintManager.addDatabaseShardingValue("sys_order", "1");
        // 设置表的分片健
        hintManager.addTableShardingValue("sys_order", "_0");
        hintManager.addTableShardingValue("sys_order", "_1");

        // 在读写分离数据库中，Hint 可以强制读主库
        //hintManager.setMasterRouteOnly();
    }
}